/**
 * 创建人:  @author huangpeng    
 * 创建时间:  2023年01月07日 14:59
 * 项目名称:  lemon-blog-cloud
 * 文件名称:  BiUserController
 * 文件描述:  @Description: 普通用户controller
 * 公司名称:  柠檬
 *
 * All rights Reserved, Designed By 柠檬 
 * @Copyright: 2022-2023
 *
 */
package com.lemon.system.controller;

import com.lemon.common.core.domain.Result;
import com.lemon.common.core.utils.StringUtils;
import com.lemon.common.core.web.controller.BaseController;
import com.lemon.common.core.web.domain.AjaxResult;
import com.lemon.common.core.web.page.TableDataInfo;
import com.lemon.common.log.annotation.Log;
import com.lemon.common.log.enums.BusinessType;
import com.lemon.common.security.annotation.InnerAuth;
import com.lemon.common.security.annotation.RequiresPermissions;
import com.lemon.common.security.utils.ExcelUtil;
import com.lemon.common.security.utils.SecurityUtils;
import com.lemon.system.api.domain.BiUser;
import com.lemon.system.api.model.LoginUser;
import com.lemon.system.service.BiUserService;
import com.lemon.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 包名称：com.lemon.system.controller
 * 类名称：BiUserController
 * 类描述：普通用户controller
 * 创建人：@author huangpeng
 * 创建时间：2023年01月07日 14:59
 */
@RestController
@RequestMapping("/baseUser")
public class BiUserController extends BaseController {

    @Autowired
    private BiUserService biUserService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询前台用户管理列表
     * @Title: list
     * @Description: 查询前台用户管理列表
     * @param biUser
     * @return: TableDataInfo
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 20:39
     */
    @RequiresPermissions("system:baseUser:list")
    @GetMapping("/list")
    public TableDataInfo list(BiUser biUser)
    {
        startPage();
        List<BiUser> list = biUserService.selectBiUserList(biUser);
        return getDataTable(list);
    }

    /**
     * 导出前台用户管理列表
     * @Title: export
     * @Description: 导出前台用户管理列表
     * @param response
     * @param biUser
     * @return: void
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 20:39
     */
    @RequiresPermissions("system:baseUser:export")
    @Log(title = "前台用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BiUser biUser)
    {
        List<BiUser> list = biUserService.selectBiUserList(biUser);
        ExcelUtil<BiUser> util = new ExcelUtil<BiUser>(BiUser.class);
        util.exportExcel(response, list, "前台用户管理数据");
    }

    /**
     * 获取前台用户管理详细信息
     * @Title: getInfo
     * @Description: 获取前台用户管理详细信息
     * @param id
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 20:39
     */
    @RequiresPermissions("system:baseUser:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(biUserService.selectByPrimaryKey(id));
    }

    /**
     * 新增前台用户管理
     * @Title: add
     * @Description: 新增前台用户管理
     * @param biUser
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 20:39
     */
    @RequiresPermissions("system:baseUser:add")
    @Log(title = "前台用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BiUser biUser)
    {
        return toAjax(biUserService.insert(biUser));
    }

    /**
     * 修改前台用户管理
     * @Title: edit
     * @Description: 修改前台用户管理
     * @param biUser
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 20:39
     */
    @RequiresPermissions("system:baseUser:edit")
    @Log(title = "前台用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BiUser biUser)
    {
        return toAjax(biUserService.update(biUser));
    }

    /**
     * 删除前台用户管理
     * @Title: remove
     * @Description: 删除前台用户管理
     * @param ids
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 20:40
     */
    @RequiresPermissions("system:baseUser:remove")
    @Log(title = "前台用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(biUserService.deleteByBatch(ids));
    }

    /**
     * 根据用户id获取普通用户列表
     * @Title: queryUserListByKey
     * @Description: 根据用户id获取普通用户列表
     * @param userIds
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/1/8 16:28
     */
    @GetMapping(value = { "/queryBaseUserListByKey", "/queryBaseUserListByKey/{userIds}" })
    public AjaxResult queryBaseUserListByKey(@PathVariable(value = "userIds", required = false) Long[] userIds) {
        List<BiUser> userList = biUserService.queryBaseUserListByKey(userIds);
        return AjaxResult.success(userList);
    }

    /**
     * 重置密码
     * @Title: resetPwd
     * @Description: 重置密码
     * @param user
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 20:28
     */
    @RequiresPermissions("system:baseUser:edit")
    @Log(title = "前台用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody BiUser user)
    {
        return toAjax(biUserService.resetPwd(user));
    }

    /**
     * @Title: register
     * @Description: 注册
     * @param user
     * @return: R<Boolean>
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/4 22:23
     */
    @InnerAuth
    @PostMapping("/register")
    public Result<BiUser> register(@RequestBody BiUser user) {
        try {
            if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
                return Result.error("当前系统没有开启注册功能！");
            }
            if (biUserService.registerUser(user)) {
                return Result.success(biUserService.selectByUserCode(user.getUserCode()));
            } else {
                return Result.error("注册失败");
            }
        } catch (Exception e) {
            logger.info("BiUserController.register.注册用户失败, msg={}", e.getMessage(), e);
            return Result.error(null, e.getMessage());
        }
    }

    /**
     * 获取登录用户信息
     * @Title: getInfo
     * @Description: 获取登录用户信息
     * @param
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/14 18:40
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        Long userId = SecurityUtils.getUserId();
        // 角色集合
        // Set<String> roles = permissionService.getRolePermission(userId);
        // 权限集合
        // Set<String> permissions = permissionService.getMenuPermission(userId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", biUserService.selectByPrimaryKey(userId));
        // ajax.put("roles", roles);
        // ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取下拉用户列表
     * @Title: getInfo
     * @Description: 获取下拉用户列表
     * @param userIds
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2022/10/30 17:34
     */
    @GetMapping(value = { "/queryUserList", "/queryUserList/{userIds}" })
    public AjaxResult getInfo(@PathVariable(value = "userIds", required = false) Long[] userIds) {
        List<BiUser> userList = biUserService.queryUserList(userIds);
        return AjaxResult.success(userList);
    }

    /**
     * 根据userCode判断当前用户信息是否存在
     * @Title: checkByUserCode
     * @Description: 根据userCode判断当前用户信息是否存在
     * @param userCode 用户登录名
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 19:25
     */
    @GetMapping("/checkByUserCode/{userCode}")
    public AjaxResult checkByUserCode(@PathVariable("userCode") String userCode)
    {
        BiUser sysUser = biUserService.selectByUserCode(userCode);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("dvExist", sysUser != null);
        return ajaxResult;
    }

    /**
     * 根据email判断当前用户信息是否存在
     * @Title: checkByEmail
     * @Description: 根据email判断当前用户信息是否存在
     * @param email 用户邮箱
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 19:25
     */
    @GetMapping("/checkByEmail/{email}")
    public AjaxResult checkByEmail(@PathVariable("email") String email)
    {
        BiUser sysUser = biUserService.selectByEmail(email);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("dvExist", sysUser != null);
        return ajaxResult;
    }

    /**
     * 根据phone判断当前用户信息是否存在
     * @Title: checkByPhone
     * @Description: 根据phone判断当前用户信息是否存在
     * @param phone 手机号码
     * @return: R<LoginUser>
     * @throws:
     * @author: huangpeng
     * @Date: 2023/5/3 19:25
     */
    @GetMapping("/checkByPhone/{phone}")
    public AjaxResult checkByPhone(@PathVariable("phone") String phone)
    {
        BiUser sysUser = biUserService.selectByPhone(phone);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("dvExist", sysUser != null);
        return ajaxResult;
    }

}
